#include "lvgl.h"
#include <stdio.h>

#ifndef WIN32
#include "ts_calibrate.h"
#include "ns2009.h"
#include "lv_port_indev.h"
#include "tslib.h"
#endif

/* Macro Definition
********************************************************************************************************/



/* Function Declaration
********************************************************************************************************/
static lv_obj_t* cross_create(lv_obj_t* p_par);


/* Structure Definition
********************************************************************************************************/
typedef struct
{
    lv_obj_t* p_obj;    // To hold all, and return to High-Level to manager mem
    lv_obj_t* p_cross[3];
    lv_obj_t* p_label;
    lv_obj_t* p_btn;
    uint8_t   count;
} win_obj_t;


/* Global Variables
********************************************************************************************************/
static const lv_point_t s_coord_5[5] = {
    {30,  30 },
    {290, 30 },
    {160, 240},
    {30,  450},
    {290, 450},
};

// TP 校正参数
static calibration s_cb_tp = {0};



/* Public Functions
********************************************************************************************************/
static void cb_clicked(lv_event_t* p_event)
{
    // lv_obj_t* p_btn = (lv_obj_t*)lv_event_get_user_data(p_event);

    // if(p_btn == NULL)
    // {
    //     return;
    // }

    // lv_obj_add_flag(p_btn, LV_OBJ_FLAG_HIDDEN);

    win_obj_t* p_win = (win_obj_t*)lv_event_get_user_data(p_event);

    if(p_win != NULL)
    {
        // uint32_t adc_x, adc_y;

        // ns2009_read_adc(&adc_x, &adc_y);
        // lv_label_set_text_fmt(p_win->p_label, "x:%d,y:%d", adc_x, adc_y);

        if(p_win->count >= 5)
        {
            return;
        }

        lv_point_t clink_point = {0};
        lv_indev_get_point(lv_indev_get_act(), &clink_point);
        lv_label_set_text_fmt(p_win->p_label, "x:%d,y:%d", clink_point.x, clink_point.y);

        s_cb_tp.xfb[p_win->count % 5] = s_coord_5[p_win->count % 5].x;
        s_cb_tp.yfb[p_win->count % 5] = s_coord_5[p_win->count % 5].y;
        s_cb_tp.x[p_win->count % 5]   = clink_point.x;
        s_cb_tp.y[p_win->count % 5]   = clink_point.y;

        lv_obj_del(p_win->p_cross[0]);

        p_win->count++;
        if(p_win->count >= 5)
        {
            perform_calibration(&s_cb_tp);

            lv_label_set_text_fmt(p_win->p_label, "Clibrate complete!\nparam:\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d",
                                  s_cb_tp.a[0], s_cb_tp.a[1], s_cb_tp.a[2], s_cb_tp.a[3], s_cb_tp.a[4], s_cb_tp.a[5], s_cb_tp.a[6]);

            ts_set_cal(&s_cb_tp);
            lv_port_indev_set_ts_clibrated(true);

            lv_obj_del(p_win->p_obj);

            // lv_refr_now(lv_disp_get_default());

            return;
        }

        p_win->p_cross[0] = cross_create(p_win->p_obj);
        lv_obj_align_to(p_win->p_cross[0], NULL, LV_ALIGN_TOP_LEFT, s_coord_5[p_win->count % 5].x - 10, s_coord_5[p_win->count % 5].y - 10);
    }
}


static void _page_obj_cb(lv_event_t* e)
{
    if(e->code == LV_EVENT_DELETE)
    {
        win_obj_t* p = (win_obj_t*)lv_event_get_user_data(e);

        if(p != NULL)
        {
            printf("mem free p_win:%x", p);
            lv_mem_free(p);
        }
    }
}


lv_obj_t* app_page_create(lv_obj_t* p_par)
{
    win_obj_t* p_win = lv_mem_alloc(sizeof(win_obj_t));

    if(p_win == NULL)
    {
        return NULL;
    }

    memset(p_win, 0, sizeof(win_obj_t));

    printf("mem malloc p_win:%x", p_win);

    p_win->p_obj = lv_obj_create(p_par);
    lv_obj_set_size(p_win->p_obj, lv_obj_get_width(p_par), lv_obj_get_height(p_par));
    lv_obj_set_scrollbar_mode(p_win->p_obj, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_border_width(p_win->p_obj, 1, 0);
    lv_obj_set_style_pad_all(p_win->p_obj, 0, 0);
    lv_obj_add_event_cb(p_win->p_obj, _page_obj_cb, LV_EVENT_DELETE, p_win);
    lv_obj_clear_flag(p_win->p_obj, LV_OBJ_FLAG_SCROLLABLE);

    p_win->p_label = lv_label_create(p_win->p_obj);
    lv_label_set_text_fmt(p_win->p_label, "w:%d, h:%d\n", lv_obj_get_width(p_par), lv_obj_get_height(p_par));


    p_win->p_cross[0] = cross_create(p_win->p_obj);
    lv_obj_align_to(p_win->p_cross[0], NULL, LV_ALIGN_TOP_LEFT, s_coord_5[0].x - 10, s_coord_5[0].y - 10);

#if 0
    p_win->p_cross[0] = cross_create(p_win->p_obj);
    lv_obj_align_to(p_win->p_cross[0], NULL, LV_ALIGN_TOP_LEFT, 10, 10);
    // lv_obj_add_event_cb(p_cross, cb_clicked, LV_EVENT_CLICKED, p_cross);

    p_win->p_cross[1] = cross_create(p_win->p_obj);
    lv_obj_align_to(p_win->p_cross[1], NULL, LV_ALIGN_TOP_RIGHT, -10, 10);
    // lv_obj_add_event_cb(p_cross, cb_clicked, LV_EVENT_CLICKED, p_cross);

    p_win->p_cross[2] = cross_create(p_win->p_obj);
    lv_obj_align_to(p_win->p_cross[2], NULL, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
    // lv_obj_add_event_cb(p_cross, cb_clicked, LV_EVENT_CLICKED, p_cross);
#endif

    p_win->p_btn = lv_obj_create(p_win->p_obj);
    lv_obj_set_size(p_win->p_btn, lv_obj_get_width(p_win->p_obj), lv_obj_get_height(p_win->p_obj));
    lv_obj_set_style_bg_opa(p_win->p_btn, LV_OPA_10, 0);
    lv_obj_add_event_cb(p_win->p_btn, cb_clicked, LV_EVENT_CLICKED, p_win);

    return p_win->p_obj;
}


/* Private Functions
********************************************************************************************************/
static void cross_cb(lv_event_t* e)
{
    if(e->code == LV_EVENT_DELETE)
    {
        lv_point_t* p = (lv_point_t*)lv_event_get_user_data(e);

        if(p != NULL)
        {
            lv_mem_free(p);
        }
    }
}

static lv_obj_t* cross_create(lv_obj_t* p_par)
{
    lv_point_t* points = lv_mem_alloc(sizeof(lv_point_t) * 4);
    if(points == NULL)
    {
        return NULL;
    }

    points[0].x = 10;
    points[0].y = 0;
    points[1].x = 10;
    points[1].y = 20;
    points[2].x = 0;
    points[2].y = 10;
    points[3].x = 20;
    points[3].y = 10;

    lv_obj_t* cont = lv_obj_create(p_par);
    lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_border_width(cont, 1, 0);
    lv_obj_set_style_pad_all(cont, 0, 0);
    lv_obj_set_size(cont, 20, 20);
    // lv_obj_align(cont, LV_ALIGN_TOP_LEFT, 10, 10);
    // lv_obj_set_user_data(cont, points);
    lv_obj_add_event_cb(cont, cross_cb, LV_EVENT_DELETE, points);

    lv_obj_t* p_cross = lv_line_create(cont);
    lv_line_set_points(p_cross, &points[0], 2);
    lv_obj_set_style_line_width(p_cross, 2, 0);

    p_cross = lv_line_create(cont);
    lv_line_set_points(p_cross, &points[2], 2);
    lv_obj_set_style_line_width(p_cross, 2, 0);

    return cont;
}


/* End Of File
********************************************************************************************************/
